#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <cmath>
#include "Shader.hpp"
#include "MeshRenderer.hpp"
#include "Texture.hpp"
#include "Time.hpp"
#include "Camera.hpp"

void frame_buff_size_callback(GLFWwindow* window, int width, int height);
void pre_render();
void render();
void process_input(GLFWwindow* window);

int main() {

    // GLFW INIT
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "Hello World", nullptr, nullptr);
    if(window == nullptr) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwSetFramebufferSizeCallback(window, frame_buff_size_callback);
    glfwMakeContextCurrent(window);

    // GLAD
    if(!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    glViewport(0, 0, 800, 600);

    pre_render();

    while (!glfwWindowShouldClose(window)) {
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // 更新时间
        Time::UpdateTime(static_cast<float>(glfwGetTime()));
        // 按键操作
        process_input(window);
        // 渲染
        render();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

void frame_buff_size_callback(GLFWwindow* window, const int width, const int height) {
    glViewport(0, 0, width, height);
}

Shader* shader;
MeshRenderer* cube_mesh_renderer;
Texture* texture1;
Texture* texture2;
Camera camera;

void pre_render() {
    shader = new Shader("Assets/Shader/07_CoordinateSystem/shader2.vert", "Assets/Shader/07_CoordinateSystem/shader2.frag");
    texture1 = new Texture("Assets/Texture/t1.png");
    texture2 = new Texture("Assets/Texture/t2.png");
    {
        float vertices[] = {
            -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
             0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
            -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
             0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
            -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

            -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
            -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
            -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
             0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
             0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
             0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
             0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
             0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
             0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

            -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
            -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
            -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
        };
        unsigned int indices[6 * 6];
        for(int i = 0; i < 6*6; i++) {
            indices[i] = i;
        }
        cube_mesh_renderer = new MeshRenderer(MeshRenderer::vertex_format_position | MeshRenderer::vertex_format_uv1);
        cube_mesh_renderer->SetVertex(vertices, std::size(vertices));
        cube_mesh_renderer->SetIndices(indices, std::size(indices));
        cube_mesh_renderer->SetShader(shader);
        cube_mesh_renderer->Init();
    }

    {
        camera.SetPosition(glm::vec3(0, 0.0, 0));
    }
}

glm::vec3 cubePositions[] = {
    glm::vec3( 0.0f,  0.0f,  -4.0f),
    glm::vec3( 2.0f,  5.0f, -15.0f),
    glm::vec3(-1.5f, -2.2f, -2.5f),
    glm::vec3(-3.8f, -2.0f, -12.3f),
    glm::vec3( 2.4f, -0.4f, -3.5f),
    glm::vec3(-1.7f,  3.0f, -7.5f),
    glm::vec3( 1.3f, -2.0f, -2.5f),
    glm::vec3( 1.5f,  2.0f, -2.5f),
    glm::vec3( 1.5f,  0.2f, -1.5f),
    glm::vec3(-1.3f,  1.0f, -1.5f)
};


void process_input(GLFWwindow* window) {
    float cameraSpeed = 3.0f * Time::deltaTime;
    if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        camera.SetPosition(camera.position + cameraSpeed * camera.front);
    if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        camera.SetPosition(camera.position - cameraSpeed * camera.front);
    if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        camera.SetPosition(camera.position - cameraSpeed * camera.right);
    if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        camera.SetPosition(camera.position + cameraSpeed * camera.right);
}

void render() {
    {

        for (int i = 0; i < std::size(cubePositions); i++) {
            cube_mesh_renderer->PreDraw();
            glEnable(GL_DEPTH_TEST);
            // 矩阵操作
            auto modelTransform = glm::mat4(1);
            modelTransform = glm::translate(modelTransform, cubePositions[i]);
            modelTransform = glm::rotate(modelTransform, static_cast<float>(glfwGetTime()), glm::vec3(1.0f, 0.0f, 0.0f));
            modelTransform = glm::rotate(modelTransform, static_cast<float>(glfwGetTime()), glm::vec3(0.0f, 1.0f, 0.0f));

            auto projectionTransform = glm::mat4(1);
            projectionTransform = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);

            cube_mesh_renderer->GetShader()->SetMat4("model", glm::value_ptr(modelTransform));
            cube_mesh_renderer->GetShader()->SetMat4("view", glm::value_ptr(camera.lookAt));
            cube_mesh_renderer->GetShader()->SetMat4("projection", glm::value_ptr(projectionTransform));

            texture1->Use(GL_TEXTURE0);
            texture2->Use(GL_TEXTURE1);
            cube_mesh_renderer->GetShader()->SetInt("ourTexture1", 0);
            cube_mesh_renderer->GetShader()->SetInt("ourTexture2", 1);

            cube_mesh_renderer->Draw();
        }
    }
}